package main

import (
	"encoding/json"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"io/ioutil"
	"os"
	"strconv"
)

type ConverterStruct struct {
	ChoiceSingle             []Question `json:"choiceSingle"`
	ChoiceMultipleIndefinite []Question `json:"choiceMultipleIndefinite"`
	Judgement                []Question `json:"judgment"`
	ChoiceMultiple           []Question `json:"choiceMultiple"`
}

func cmain() {
	jsonFile, err := os.Open("./res1.json")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func(jsonFile *os.File) {
		err := jsonFile.Close()
		if err != nil {
			return
		}
	}(jsonFile)
	byteValue, _ := ioutil.ReadAll(jsonFile)
	var converter ConverterStruct
	err = json.Unmarshal(byteValue, &converter)
	if err != nil {
		fmt.Println(err)
		return
	}
	e := excelize.NewFile()
	indexSheet := e.NewSheet("Sheet1")
	index := 1
	for _, item := range converter.ChoiceSingle {
		for _, nitem := range item.Options {
			strIndex := strconv.Itoa(index)
			e.SetCellValue("Sheet1", "A"+strIndex, item.ID)
			e.SetCellValue("Sheet1", "B"+strIndex, nitem.Content)
			e.SetCellValue("Sheet1", "C"+strIndex, nitem.Weight)
			e.SetCellValue("Sheet1", "D"+strIndex, item.Type)
			index++
		}
	}
	for _, item := range converter.ChoiceMultipleIndefinite {
		for _, nitem := range item.Options {
			strIndex := strconv.Itoa(index)
			e.SetCellValue("Sheet1", "A"+strIndex, item.ID)
			e.SetCellValue("Sheet1", "B"+strIndex, nitem.Content)
			e.SetCellValue("Sheet1", "C"+strIndex, nitem.Weight)
			e.SetCellValue("Sheet1", "D"+strIndex, item.Type)
			index++
		}
	}
	for _, item := range converter.ChoiceMultiple {
		for _, nitem := range item.Options {
			strIndex := strconv.Itoa(index)
			e.SetCellValue("Sheet1", "A"+strIndex, item.ID)
			e.SetCellValue("Sheet1", "B"+strIndex, nitem.Content)
			e.SetCellValue("Sheet1", "C"+strIndex, nitem.Weight)
			e.SetCellValue("Sheet1", "D"+strIndex, item.Type)
			index++
		}
	}
	//for _, item := range converter.Judgement {
	//	strIndex := strconv.Itoa(index)
	//	e.SetCellValue("Sheet1", "A"+strIndex, item.ID)
	//	e.SetCellValue("Sheet1", "B"+strIndex, item.Title)
	//	e.SetCellValue("Sheet1", "C"+strIndex, item.Answer)
	//	e.SetCellValue("Sheet1", "D"+strIndex, item.Type)
	//	index++
	//}
	for _, item := range converter.Judgement {
		for _, nitem := range item.Options {
			strIndex := strconv.Itoa(index)
			e.SetCellValue("Sheet1", "A"+strIndex, item.ID)
			e.SetCellValue("Sheet1", "B"+strIndex, nitem.Content)
			e.SetCellValue("Sheet1", "C"+strIndex, nitem.Weight)
			e.SetCellValue("Sheet1", "D"+strIndex, item.Type)
			index++
		}
	}
	e.SetActiveSheet(indexSheet)

	if err := e.SaveAs("./res2.xlsx"); err != nil {
		fmt.Println(err)
	}
}
